#!/usr/bin/ python
# -*- coding: utf-8 -*-
import json
import re
import  time

import requests

headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, "
                          "like Gecko) Chrome/90.0.4430.212 Safari/537.36",
            "cookie":''
        }

mow_millisecond = int(round(time.time() * 1000))

def getCode():
    base_url = 'https://www.shipmentlink.com'

    url = 'https://www.shipmentlink.com/servlet/TUF1_CaptchaUtils?d='+str(mow_millisecond)
    photo = requests.get(url, headers=headers)
    # filename = basepath + '1.jpg'
    print('mow_millisecond',mow_millisecond)
    filename =  '1.jpg'
    with open(filename, 'wb') as f:
        f.write(photo.content)


def post_code():
    url = 'https://www.shipmentlink.com/servlet/TDB1_CargoTracking.do'
    url = 'https://www.evergreen-shipping.cn/gaia/CT_MENU1?TYPE=BL&BL=146900296776'

    mow_millisecond = 1621435886970
    m_str = 'WSRS'
    # data = {
    #     'BL':'146900296776',
    #     'CNTR':'',
    #     'bkno':'',
    #     'TYPE':'BL',
    #     # 'NO':'',
    #     # 'NO':'',
    #     # 'NO':'',
    #     # 'NO':'',
    #     # 'NO':'',
    #     'SEL':'s_bl',
    #     'NO':'146900296776',
    #     'captcha_input':str(m_str),
    #     'hd_captcha_seq':str(mow_millisecond),
    # }
    data = {
        'tdb1_qc_type':'Tracking',
        'tdb1_qc_type_value':'Tracking',
        'tdb1_qctrack_type':'BL',
        'tdb1_qctrack_no':'146900296776',
        'departureMonth':'05',
        'departureDay':'19',
        'departureYear':'2021',
        'departureDate':'20210519',
        'durationWeek':'14',
        'CheckKey':'',
        'captcha_input':m_str,
        'hd_captcha_seq':mow_millisecond,
    }
    resp = requests.post(url, headers=headers,data=json.dumps(data),verify=False)
    with open('a.html', 'wb') as f:
        f.write(resp.content)
    print(resp.text)

headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, "
                          "like Gecko) Chrome/90.0.4430.212 Safari/537.36",
            "cookie":''
        }
def get_cookie():
    url = 'https://www.shipmentlink.com/servlet/TDB1_CargoTracking.do'
    req = requests.get(url, headers=headers)
    cookies = requests.utils.dict_from_cookiejar(req.cookies)  # 转成字典格式
    print(cookies,type(cookies))
    s = cookies['JSESSIONID']
    print(s)
    return  cookies

def get_html():

    data = ''
    # with open('a.html','r',encoding='utf-8') as f:
    #     for line in f.readlines():
    #         line = line.strip()
    #         data +=line

    f = open("a.html", "r", encoding="utf-8")
    data = f.read()
    # infos = re.findall('.*?class="f12rown1" nowrap>(.*?)</td>', data, re.S|re.M)
    t = re.findall(r'.*?class="f12rown1" nowrap>(.*?)</td>',data,re.S|re.M)
    print(t)
    # print(data)

if __name__ == '__main__':
    # getCode()
    get_cookie()
    # post_code()
    # get_html()